package com.mj.activity;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.mj.R;
import com.mj.base.BaseActivity;
import com.mj.bean.Album;
import com.mj.view.HackyViewPager;

/***
 * 相册大图展示页
 * 
 * @author Duan
 * 
 */
public class Activity_BigPics extends BaseActivity {
	private ArrayList<Album> albums = new ArrayList<Album>();
	private static final String ISLOCKED_ARG = "isLocked";
	private ViewPager viewPager;
	private int position;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_bigpic);
		String title = getIntent().getStringExtra("title");
		// title
		aq.id(R.id.btn_left).clicked(this, "back");
		aq.id(R.id.btn_right).gone();
		aq.id(R.id.tv_title).text(title);
		initViewPager(arg0);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		if (isViewPagerActive()) {
			outState.putBoolean(ISLOCKED_ARG, ((HackyViewPager) viewPager).isLocked());
		}
		super.onSaveInstanceState(outState);
	}

	/***
	 * 初始化UI
	 * 
	 * @param arg0
	 */
	@SuppressWarnings("unchecked")
	private void initViewPager(Bundle arg0) {
		albums = (ArrayList<Album>) getIntent().getSerializableExtra("albums");
		position = getIntent().getIntExtra("position", 0);
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		viewPager.setAdapter(new PicPagerAdapter());
		viewPager.setCurrentItem(position);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				PhotoView photoView = (PhotoView) viewPager.getChildAt(position);
				if (photoView != null) {
					photoView.setScale(photoView.getMinimumScale());
				}
				position = arg0;
			}
		});
		if (arg0 != null) {
			boolean isLocked = arg0.getBoolean(ISLOCKED_ARG, false);
			((HackyViewPager) viewPager).setLocked(isLocked);
		}
	}

	class PicPagerAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return albums.size();
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());
			aq.id(photoView).image(albums.get(position).getImg());
			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}

	/***
	 * viewpager是否激活
	 * 
	 * @return
	 */
	private boolean isViewPagerActive() {
		return (viewPager != null && viewPager instanceof HackyViewPager);
	}

}
